package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.OldmanPhotosEntity;
import city.spring.modules.reception.service.OldmanPhotosService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：照片
 *
 * @author pxm
 * @date 2020-07-06 16:44:54
 */
@RestController
@RequestMapping("/oldman_photos")
public class OldmanPhotosController extends ApiController {
    private final OldmanPhotosService oldmanPhotosService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanPhotosEntity, ?>> entityOrderFields;

    public OldmanPhotosController(OldmanPhotosService oldmanPhotosService) {
        this.oldmanPhotosService = oldmanPhotosService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanPhotosEntity::getId);
        entityOrderFields.add(OldmanPhotosEntity::getAlbumId);
        entityOrderFields.add(OldmanPhotosEntity::getName);
        entityOrderFields.add(OldmanPhotosEntity::getPhotoDate);
        entityOrderFields.add(OldmanPhotosEntity::getDescribes);
        entityOrderFields.add(OldmanPhotosEntity::getOldmanId);
        entityOrderFields.add(OldmanPhotosEntity::getImgUrl);
        entityOrderFields.add(OldmanPhotosEntity::getGmtCreate);
        entityOrderFields.add(OldmanPhotosEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>照片</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanPhotosEntity entity) {
        LambdaQueryChainWrapper<OldmanPhotosEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>照片</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanPhotosEntity entity) {
        LambdaQueryChainWrapper<OldmanPhotosEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanPhotosEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>照片</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanPhotosService.getById(id));
    }

    /**
     * 添加一个 <strong>照片</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanPhotosEntity entity) {
        oldmanPhotosService.saveOldmanPhotos(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>照片</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanPhotosEntity entity) {
        entity.setId(id);
        oldmanPhotosService.updateOldmanPhotos(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>照片</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanPhotosService.deleteOldmanPhotos(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>照片</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanPhotosService.deleteOldmanPhotos(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanPhotosEntity> buildLambdaQuery(OldmanPhotosEntity entity) {
        LambdaQueryChainWrapper<OldmanPhotosEntity> lambdaQuery = oldmanPhotosService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanPhotosEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAlbumId()), OldmanPhotosEntity::getAlbumId, entity.getAlbumId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), OldmanPhotosEntity::getName, entity.getName());
        lambdaQuery.eq(entity.getPhotoDate() != null, OldmanPhotosEntity::getPhotoDate, entity.getPhotoDate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescribes()), OldmanPhotosEntity::getDescribes, entity.getDescribes());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanPhotosEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getImgUrl()), OldmanPhotosEntity::getImgUrl, entity.getImgUrl());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanPhotosEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanPhotosEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanPhotosEntity> buildQuery(OldmanPhotosEntity entity) {
        QueryWrapper<OldmanPhotosEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getAlbumId()), "album_id", entity.getAlbumId());
        wrapper.like(StringUtils.isNotBlank(entity.getName()), "name", entity.getName());
        wrapper.eq(entity.getPhotoDate() != null, "photo_date", entity.getPhotoDate());
        wrapper.like(StringUtils.isNotBlank(entity.getDescribes()), "describes", entity.getDescribes());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getImgUrl()), "img_url", entity.getImgUrl());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
